<template>
    <table cellspacing="0" cellpadding="0" border="0" :style="styleObject">
        <colgroup>
            <col v-for="(column, index) in columns" :width="setCellWidth(column, index, false)">
        </colgroup>
        <tbody :class="[prefixCls + '-tbody']">
        <template v-for="(row, index) in rebuildData">
            <tr
                :key="row"
                :class="rowClasses(row._index)"
                @mouseenter.stop="handleMouseIn(row._index)"
                @mouseleave.stop="handleMouseOut(row._index)"
                @click.stop="clickCurrentRow(row._index)"
                @dblclick.stop="dblclickCurrentRow(row._index)">
                <td v-for="column in columns" :class="alignCls(column, row)"
                    :rowspan="row[column.key+'__rowspan']" v-if="!row[column.key+'__rowHide']">
                    <Cell
                        :fixed="fixed"
                        :prefix-cls="prefixCls"
                        :row="row"
                        :column="column"
                        :natural-index="index"
                        :index="row._index"
                        :checked="rowChecked(row._index)"
                        :disabled="rowDisabled(row._index)"
                        :expanded="rowExpanded(row._index)"
                    ></Cell>
                </td>
            </tr>
            <tr v-if="rowExpanded(row._index)">
                <td :colspan="columns.length" :class="prefixCls + '-expanded-cell'">
                    <Expand :row="row" :render="expandRender" :index="row._index"></Expand>
                </td>
            </tr>
        </template>
        </tbody>
    </table>
</template>
<script>
    // todo :key="row"
    import Cell from 'iview/src/components/table/cell.vue';
    import Expand from 'iview/src/components/table/expand';
    import Mixin from 'iview/src/components/table/mixin';

    function combineCell(columns,data) {
        let temp = {...data[0]};
        delete temp._index;
        columns.forEach(function (value) {
            if(!value.__rowspan){
                delete  temp[value.key]
            }
        });
        let keys = Object.keys(temp);

        data = data.map(function (value) {
            return {...value};
        }).slice(0,100);
        let flag;
        for (let f = 0 ; f < keys.length;f++) {
            let field = keys[f];
            let k = 0;
            flag = false;
            while (k < data.length) {
                data[k][field + '__rowspan'] = 1;
                data[k][field + '__rowHide'] = false;
                let i = k + 1;
                for (; i <= data.length - 1; i++) {
                    if (f > 0 && data[i][keys[f - 1]] !== data[i - 1][keys[f - 1]]) {
                        break;
                    }
                    if (data[k][field] === data[i][field] && data[k][field] !== '') {
                        flag = true;
                        data[k][field + '__rowspan']++;
                        data[k][field + '__rowHide'] = false;
                        data[i][field + '__rowspan'] = 1;
                        data[i][field + '__rowHide'] = true;
                    } else {
                        break;
                    }
                }
                k = i;
            }
            //如果为假不再行合并
            if(!flag){
                break;
            }
        }
        return data;
    }

    /**
     * props说明:
     * columns[{
     *  __rowspan,//为真时需要合并行
     * }]
     * data[{
     *  __subtotal,//为真时表明此行为小计行
     * }]
     */
    export default {
        name: 'TableBody',
        mixins: [ Mixin ],
        components: { Cell, Expand },
        props: {
            prefixCls: String,
            styleObject: Object,
            columns: Array,
            data: Array,    // rebuildData
            objData: Object,
            columnsWidth: Object,
            fixed: {
                type: [Boolean, String],
                default: false
            }
        },
        computed: {
            rebuildData:function () {
                return this.data.length >1 ? combineCell(this.columns,this.data) : this.data;
            },
            expandRender () {
                let render = function () {
                    return '';
                };
                for (let i = 0; i < this.columns.length; i++) {
                    const column = this.columns[i];
                    if (column.type && column.type === 'expand') {
                        if (column.render) render = column.render;
                    }
                }
                return render;
            }
        },
        methods: {
            rowClasses (_index) {
                return [
                    `${this.prefixCls}-row`,
                    this.rowClsName(_index),
                    {
                        [`${this.prefixCls}-row-highlight`]: this.objData[_index] && this.objData[_index]._isHighlight,
                        [`${this.prefixCls}-row-hover`]: this.objData[_index] && this.objData[_index]._isHover
                    }
                ];
            },
            rowChecked (_index) {
                return this.objData[_index] && this.objData[_index]._isChecked;
            },
            rowDisabled(_index){
                return this.objData[_index] && this.objData[_index]._isDisabled;
            },
            rowExpanded(_index){
                return this.objData[_index] && this.objData[_index]._isExpanded;
            },
            rowClsName (_index) {
                return this.$parent.rowClassName(this.objData[_index], _index);
            },
            handleMouseIn (_index) {
                this.$parent.handleMouseIn(_index);
            },
            handleMouseOut (_index) {
                this.$parent.handleMouseOut(_index);
            },
            clickCurrentRow (_index) {
                this.$parent.clickCurrentRow(_index);
            },
            dblclickCurrentRow (_index) {
                this.$parent.dblclickCurrentRow(_index);
            }
        }
    };
</script>
